<?php
namespace Modules\Comment\Library;

use Modules\Region\Entity\Block as BlockEntity;
use Phalcon\Di;

class Block
{
    public static function commentList(BlockEntity $blockEntity)
    {
        $di = Di::getDefault();
        $data = $blockEntity->getData();
        $query = [
            'sort' => [],
            'limit' => $data['limit'],
            'skip' => 0,
        ];
        if (isset($data['sort'])) {
            switch ($data['sort']) {
                case 'hot':
                    $query['sort'] = [
                        'hot' => -1,
                    ];
                    break;
                case 'new':
                    $query['sort'] = [
                        'created' => -1,
                    ];
                    break;
            }
        }
        if (isset($data['commentContentModel'])) {
            if (is_string($data['commentContentModel'])) {
                $query['conditions']['contentModel'] = $data['commentContentModel'];
            } elseif (is_array($data['commentContentModel'])) {
                $query['conditions']['contentModel'] = ['$in' => array_values($data['commentContentModel'])];
            }
        }

        $commentList = $di->getShared('entityManager')->find('comment', $query);
        return [
            '#templates' => [
                'block-commentList',
                'block-' . $blockEntity->getId()
            ],
            '#module' => 'comment',
            'data' => $blockEntity,
            'commentList' => $commentList,
        ];
    }
}